package ribbitmq_4;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

public class producer_4 {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory c = new ConnectionFactory();
        c.setHost("192.168.64.140");
        c.setPort(5672);
        c.setUsername("admin");
        c.setPassword("admin");

        Channel ch = c.newConnection().createChannel();
        //方式二 ch.exchangeDeclare("logs", "fanout")
        ch.exchangeDeclare("logsDIRECT", BuiltinExchangeType.DIRECT);

        //定义循环发送消息
        while (true){
            System.out.println("请输入消息");
            String msg = new Scanner(System.in).nextLine();

            System.out.println("请输入陆游建关键词");
            String key = new Scanner(System.in).nextLine();

            ch.basicPublish("logsDIRECT",//交换机名
                    key,    //陆游建关键词
                    null,      //其他属性消息持久化 MessageProperties.PERSISTENT_TEXT_PLAIN
                    msg.getBytes()); //转换字节

            System.out.println("发生成功");
        }
    }
}
